package com.ruoyi.customerSample.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;

import com.ruoyi.customerSample.domain.SampleDetailBill;
import com.ruoyi.customerSample.domain.SampleReceptionBill;
import com.ruoyi.customerSample.service.ISampleDetailBillService;
import com.ruoyi.customerSample.service.ISampleReceptionBillService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 样品接收单据Controller
 * 
 * @author ruoyi
 * @date 2023-04-10
 */
@RestController
@RequestMapping("/sample/bill")
@Api(value="样品接收单据",tags = "样品接收单据接口")
public class SampleReceptionBillController extends BaseController
{
    @Autowired
    private ISampleReceptionBillService sampleReceptionBillService;

    @Autowired
    private ISampleDetailBillService sampleDetailBillService;

    /**
     * 查询样品接收单据列表
     */
    @GetMapping("/list")
    @ApiOperation(value="查询样品接收单据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "每页记录数", required = true, dataType = "String"),
    })
    public TableDataInfo list(SampleReceptionBill sampleReceptionBill)
    {
        startPage();
        List<SampleReceptionBill> list = sampleReceptionBillService.selectSampleReceptionBillList(sampleReceptionBill);
        return getDataTable(list);
    }

    /**
     * 导出样品接收单据列表
     */
    @Log(title = "样品接收单据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SampleReceptionBill sampleReceptionBill)
    {
        List<SampleReceptionBill> list = sampleReceptionBillService.selectSampleReceptionBillList(sampleReceptionBill);
        ExcelUtil<SampleReceptionBill> util = new ExcelUtil<SampleReceptionBill>(SampleReceptionBill.class);
        return util.exportExcel(list, "bill");
    }

    /**
     * 获取样品接收单据详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value="获取样品接收单据详细信息")
    @ApiImplicitParam(paramType="query", name = "id", value = "主键", required = true, dataType = "String")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sampleReceptionBillService.selectSampleReceptionBillById(id));
    }

    /**
     * 根据项目编号获取需要样品明细
     */
    @GetMapping("/getDetailByItemNum/{itemNum}")
    @ApiOperation(value="根据项目编号获取需要样品明细")
    @ApiImplicitParam(paramType="query", name = "itemNum", value = "项目编号", required = true, dataType = "String")
    public AjaxResult getDetailByItemNum(@PathVariable("itemNum") String itemNum) {
        //根据项目编号获取来样单
        SampleReceptionBill sampleReceptionBill = sampleReceptionBillService.selectSampleReceptionBillByItemNum(itemNum);
        //根据样品id和是否退样状态获取详情数据
        SampleDetailBill sampleDetailBill = new SampleDetailBill();
        sampleDetailBill.setSid(sampleReceptionBill.getId());
        List<SampleDetailBill> list = sampleDetailBillService.selectSampleDetailBillList(sampleDetailBill);
        sampleReceptionBill.setList(list);
        return AjaxResult.success(list);
    }

    /**
     * 获取所有来样登记数据
     */
    @GetMapping("/allSampleInfo")
    @ApiOperation(value="获取所有来样登记数据")
    public AjaxResult allSampleInfo(@ApiParam(name="受检单位名称",value="传入字符串格式",required=true)@RequestParam String  client)
    {
        return AjaxResult.success(sampleReceptionBillService.selectAllSampleBill(client));
    }

    /**
     * 获取所有项目编号来样登记数据
     */
    @GetMapping("/allItemNumSampleInfo")
    @ApiOperation(value="获取所有项目编号来样登记数据")
    public AjaxResult allItemNumSampleInfo(@ApiParam(name="项目编号",value="传入字符串格式",required=true)@RequestParam String  itemNum)
    {
        return AjaxResult.success(sampleReceptionBillService.selectAllSampleBillByNum(itemNum));
    }

    /**
     * 新增样品接收单据
     */
    @Log(title = "样品接收单据", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value="新增样品接收单据")
    public AjaxResult add(@ApiParam(name="样品接收单据",value="传入json格式",required=true)@RequestBody SampleReceptionBill sampleReceptionBill)
    {
        return toAjax(sampleReceptionBillService.insertSampleReceptionBill(sampleReceptionBill));
    }

    /**
     * 修改样品接收单据
     */
    @Log(title = "样品接收单据", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value="修改样品接收单据")
    public AjaxResult edit(@ApiParam(name="样品接收单据",value="传入json格式",required=true)@RequestBody SampleReceptionBill sampleReceptionBill)
    {
        return toAjax(sampleReceptionBillService.updateSampleReceptionBill(sampleReceptionBill));
    }

    /**
     * 删除样品接收单据
     */
    @Log(title = "样品接收单据", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value="删除样品接收单据")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sampleReceptionBillService.deleteSampleReceptionBillByIds(ids));
    }
}
